if (-not $env:PACKAGE_VERSION) { $env:PACKAGE_VERSION = "v99.99.99" } # 默认值
$PACKAGE_VERSION = $env:PACKAGE_VERSION.Substring(1) # 去掉 v

echo "Downloading & unpacking BALTAM_CORE component ..."
# core
Invoke-RestMethod -Headers @{ "JOB-TOKEN"="$CI_JOB_TOKEN" } -uri "${CI_API_V4_URL}/projects/14/packages/generic/baltam_core/v$BALTAM_CORE_VERSION/BALTAM_CORE-$BALTAM_CORE_VERSION-win64-core.zip" -Method get -OutFile BALTAM_CORE-$BALTAM_CORE_VERSION-win64-core.zip
Invoke-RestMethod -Headers @{ "JOB-TOKEN"="$CI_JOB_TOKEN" } -uri "${CI_API_V4_URL}/projects/14/packages/generic/baltam_core/v$BALTAM_CORE_VERSION/BALTAM_CORE-$BALTAM_CORE_VERSION-win64-dev.zip" -Method get -OutFile BALTAM_CORE-$BALTAM_CORE_VERSION-win64-dev.zip
# builtin_runtime
Invoke-RestMethod -Headers @{ "JOB-TOKEN"="$CI_JOB_TOKEN" } -uri "${CI_API_V4_URL}/projects/$BUILTIN_RUNTIME_PROJECT_ID/packages/generic/builtin_runtime/$BUILTIN_RUNTIME_VERSION/builtin_runtime-$BUILTIN_RUNTIME_VERSION-win64-main.zip" -Method get -OutFile builtin_runtime-$BUILTIN_RUNTIME_VERSION-win64-main.zip

if(!$?) { Exit $LASTEXITCODE }
dir .
mkdir -p deps\core
7z x -y -bsp1 .\BALTAM_CORE-$BALTAM_CORE_VERSION-win64-core.zip -odeps\core\
7z x -y -bsp1 .\BALTAM_CORE-$BALTAM_CORE_VERSION-win64-dev.zip -odeps\core\
7z x -y -bsp1 .\builtin_runtime-$BUILTIN_RUNTIME_VERSION-win64-main.zip -odeps\builtin_runtime\
if(!$?) { Exit $LASTEXITCODE }

echo "Compiling the code..."
mkdir build 
cd build
C:\\mnt\msys2\msys2_shell.cmd -defterm -here -no-start -mingw64 -c "cmake -G 'MinGW Makefiles' -DCMAKE_BUILD_TYPE=Release -DCODEGEN_TOOLS_VER=${PACKAGE_VERSION} .."
if(!$?) { Exit $LASTEXITCODE }
C:\\mnt\msys2\msys2_shell.cmd -defterm -here -no-start -mingw64 -c  "mingw32-make -j 4"
if(!$?) { Exit $LASTEXITCODE }
echo "Compile complete."
C:\\mnt\msys2\msys2_shell.cmd -defterm -here -no-start -mingw64 -c  "mingw32-make package"
if(!$?) { Exit $LASTEXITCODE }

